package com.moonbird.springboot.service;

import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.moonbird.springboot.DTO.Result;
import com.moonbird.springboot.Utils.AudioTranscriptionUtils;
import com.moonbird.springboot.Utils.Qwen.AudioTranscriptionUtil;
import com.moonbird.springboot.Utils.Qwen.QwenTurboUtil;
import com.moonbird.springboot.Utils.Qwen.TextToSpeechUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

import static com.moonbird.springboot.Utils.Qwen.ImageGeneratorUtil.generateImageBase64;

@Service
public class QwenService {

    private List<Message> history = new ArrayList<>();

    public Result<String> chat(String userMessage) throws NoApiKeyException, InputRequiredException {
        // 添加用户消息到历史记录
        Message userMsg = Message.builder().role("user").content(userMessage).build();
        history.add(userMsg);

        // 调用QwenTurboUtil类生成响应
        String response = QwenTurboUtil.generateResponse(history);

        // 添加助手的响应到历史记录
        Message assistantMsg = Message.builder().role("assistant").content(response).build();
        history.add(assistantMsg);

        return Result.success(response);
    }

    public Result<String> toAudio(String aiudioMessage) {
        //文本转语音
        try {
            String text = aiudioMessage;
            String base64Audio = TextToSpeechUtils.syncAudioDataToBase64(text);
            return Result.success(base64Audio);
        } catch (Exception e) {
            System.out.println("Error generating audio: " + e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    public Result<String> createImage(String text) {
        try {
            String base64Image = generateImageBase64(text);
            //System.out.println(base64Image);
            //System.exit(0);
            return Result.success(base64Image);
        } catch (ApiException | NoApiKeyException | IOException e) {
            //System.out.println(e.getMessage());
            //System.exit(0);
            return Result.fail(e.getMessage());
        }

    }

    public Result<String> transcribeAudio(MultipartFile file) throws IOException {
        try {
            // Save the uploaded file to a temporary location
            File tempFile = new File("temp_audio.wav");
            Files.copy(file.getInputStream(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

            // Transcribe the audio file
            String transcribedText = AudioTranscriptionUtils.transcribeAudio(tempFile);

            // Clean up the temporary file
            tempFile.delete();

            return Result.success(transcribedText);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("Error processing the audio file.");
        }
    }
}
